//
//  iTunesSensor.m
//  Pennyworth
//
//  Created by Chris Karr on 12/24/07.
//  Copyright 2007 Chris J. Karr. All rights reserved.
//

#import "iTunesSensor.h"
#import "Context.h"
#import "SensorsController.h"

#define SENSOR_NAME @"iTunes"

@implementation iTunesSensor

- (void) update:(NSNotification *) notification
{
	if (![[SensorsController sharedController] canFire:SENSOR_NAME])
	{
		NSMutableDictionary * note = [NSMutableDictionary dictionary];
		
		[note setValue:@"iTunes Artist" forKey:OBSERVATION_SENSOR];
		[[NSNotificationCenter defaultCenter] postNotificationName:OBSERVATION_UPDATE object:self userInfo:note];
		
		[note setValue:@"iTunes Album" forKey:OBSERVATION_SENSOR];
		[[NSNotificationCenter defaultCenter] postNotificationName:OBSERVATION_UPDATE object:self userInfo:note];
		
		[note setValue:@"iTunes Track Name" forKey:OBSERVATION_SENSOR];
		[[NSNotificationCenter defaultCenter] postNotificationName:OBSERVATION_UPDATE object:self userInfo:note];
		
		[note setValue:@"iTunes Playlist" forKey:OBSERVATION_SENSOR];
		[[NSNotificationCenter defaultCenter] postNotificationName:OBSERVATION_UPDATE object:self userInfo:note];
		
		[note setValue:@"iTunes Genre" forKey:OBSERVATION_SENSOR];
		[[NSNotificationCenter defaultCenter] postNotificationName:OBSERVATION_UPDATE object:self userInfo:note];
		
		return;
	}		
	
	BOOL iTunesOpen = NO;
	
	NSWorkspace * workspace = [NSWorkspace sharedWorkspace];

	NSDictionary * active = [workspace activeApplication];

	if ([[active valueForKey:@"NSApplicationName"] isEqual:@"iTunes"])
		iTunesOpen = YES;

	NSArray * apps = [workspace launchedApplications];
	
	int i = 0;
	for (i = 0; i < [apps count]; i++)
	{
		if ([[[apps objectAtIndex:i] valueForKey:@"NSApplicationName"] isEqual:@"iTunes"])
			iTunesOpen = YES;
	}
	
	// NSDictionary * userInfo = [notification userInfo];

	if (iTunesOpen)
	{
		NSAppleScript * script = [[NSAppleScript alloc] initWithSource:@"tell application \"iTunes\"\n\
																			player state\n\
																		end tell"];

		NSAppleEventDescriptor * desc = [script executeAndReturnError:nil];

		NSString * descString = [desc stringValue];

		if (desc != nil && [desc stringValue] != nil)
		{
			if ([descString isEqual:@"kPSp"])
				descString = @"Paused";
			else if ([descString isEqual:@"kPSP"])
				descString = @"Playing";
			else if ([descString isEqual:@"kPSS"])
				descString = @"Stopped";
				
			NSMutableDictionary * note = [NSMutableDictionary dictionary];
	
			[note setValue:@"iTunes State" forKey:OBSERVATION_SENSOR];
			[note setValue:descString forKey:OBSERVATION_OBSERVATION];

			[[NSNotificationCenter defaultCenter] postNotificationName:OBSERVATION_UPDATE object:self userInfo:note];
		}
		
		[script release];
		
		if (!([descString isEqual:@"Playing"] || [descString isEqual:@"Paused"]))
		{
			NSMutableDictionary * note = [NSMutableDictionary dictionary];
			[note setValue:nil forKey:OBSERVATION_OBSERVATION];
	
			[note setValue:@"iTunes Artist" forKey:OBSERVATION_SENSOR];
			[[NSNotificationCenter defaultCenter] postNotificationName:OBSERVATION_UPDATE object:self userInfo:note];

			[note setValue:@"iTunes Album" forKey:OBSERVATION_SENSOR];
			[[NSNotificationCenter defaultCenter] postNotificationName:OBSERVATION_UPDATE object:self userInfo:note];

			[note setValue:@"iTunes Track Name" forKey:OBSERVATION_SENSOR];
			[[NSNotificationCenter defaultCenter] postNotificationName:OBSERVATION_UPDATE object:self userInfo:note];

			[note setValue:@"iTunes Playlist" forKey:OBSERVATION_SENSOR];
			[[NSNotificationCenter defaultCenter] postNotificationName:OBSERVATION_UPDATE object:self userInfo:note];

			[note setValue:@"iTunes Genre" forKey:OBSERVATION_SENSOR];
			[[NSNotificationCenter defaultCenter] postNotificationName:OBSERVATION_UPDATE object:self userInfo:note];
		}
		else
		{
			script = [[NSAppleScript alloc] initWithSource:@"tell application \"iTunes\"\n\
																			artist of current track\n\
																		end tell"];

			desc = [script executeAndReturnError:nil];
		
			if (desc != nil && [desc stringValue] != nil)
			{
				NSMutableDictionary * note = [NSMutableDictionary dictionary];
	
				[note setValue:@"iTunes Artist" forKey:OBSERVATION_SENSOR];
				[note setValue:[desc stringValue] forKey:OBSERVATION_OBSERVATION];

				[[NSNotificationCenter defaultCenter] postNotificationName:OBSERVATION_UPDATE object:self userInfo:note];
			}
		
			[script release];

			script = [[NSAppleScript alloc] initWithSource:@"tell application \"iTunes\"\n\
																			album of current track\n\
																		end tell"];

			desc = [script executeAndReturnError:nil];
		
			if (desc != nil && [desc stringValue] != nil)
			{
				NSMutableDictionary * note = [NSMutableDictionary dictionary];
	
				[note setValue:@"iTunes Album" forKey:OBSERVATION_SENSOR];
				[note setValue:[desc stringValue] forKey:OBSERVATION_OBSERVATION];

				[[NSNotificationCenter defaultCenter] postNotificationName:OBSERVATION_UPDATE object:self userInfo:note];
			}

			[script release];

			script = [[NSAppleScript alloc] initWithSource:@"tell application \"iTunes\"\n\
																			name of current track\n\
																		end tell"];

			desc = [script executeAndReturnError:nil];
		
			if (desc != nil && [desc stringValue] != nil)
			{
				NSMutableDictionary * note = [NSMutableDictionary dictionary];
	
				[note setValue:@"iTunes Track Name" forKey:OBSERVATION_SENSOR];
				[note setValue:[desc stringValue] forKey:OBSERVATION_OBSERVATION];

				[[NSNotificationCenter defaultCenter] postNotificationName:OBSERVATION_UPDATE object:self userInfo:note];
			}
		
			[script release];

			script = [[NSAppleScript alloc] initWithSource:@"tell application \"iTunes\"\n\
																			genre of current track\n\
																		end tell"];

			desc = [script executeAndReturnError:nil];
		
			if (desc != nil && [desc stringValue] != nil)
			{
				NSMutableDictionary * note = [NSMutableDictionary dictionary];
	
				[note setValue:@"iTunes Genre" forKey:OBSERVATION_SENSOR];
				[note setValue:[desc stringValue] forKey:OBSERVATION_OBSERVATION];

				[[NSNotificationCenter defaultCenter] postNotificationName:OBSERVATION_UPDATE object:self userInfo:note];
			}
		
			[script release];

			script = [[NSAppleScript alloc] initWithSource:@"tell application \"iTunes\"\n\
																			name of current playlist\n\
																		end tell"];

			desc = [script executeAndReturnError:nil];
		
			if (desc != nil && [desc stringValue] != nil)
			{
				NSMutableDictionary * note = [NSMutableDictionary dictionary];
	
				[note setValue:@"iTunes Playlist" forKey:OBSERVATION_SENSOR];
				[note setValue:[desc stringValue] forKey:OBSERVATION_OBSERVATION];

				[[NSNotificationCenter defaultCenter] postNotificationName:OBSERVATION_UPDATE object:self userInfo:note];
			}
		
			[script release];
		}
	}
	else
	{
		NSMutableDictionary * note = [NSMutableDictionary dictionary];
		[note setValue:nil forKey:OBSERVATION_OBSERVATION];
	
		[note setValue:@"iTunes Artist" forKey:OBSERVATION_SENSOR];
		[[NSNotificationCenter defaultCenter] postNotificationName:OBSERVATION_UPDATE object:self userInfo:note];

		[note setValue:@"iTunes Album" forKey:OBSERVATION_SENSOR];
		[[NSNotificationCenter defaultCenter] postNotificationName:OBSERVATION_UPDATE object:self userInfo:note];

		[note setValue:@"iTunes Track Name" forKey:OBSERVATION_SENSOR];
		[[NSNotificationCenter defaultCenter] postNotificationName:OBSERVATION_UPDATE object:self userInfo:note];

		[note setValue:@"iTunes Playlist" forKey:OBSERVATION_SENSOR];
		[[NSNotificationCenter defaultCenter] postNotificationName:OBSERVATION_UPDATE object:self userInfo:note];

		[note setValue:@"iTunes Genre" forKey:OBSERVATION_SENSOR];
		[[NSNotificationCenter defaultCenter] postNotificationName:OBSERVATION_UPDATE object:self userInfo:note];

		[note setValue:@"iTunes State" forKey:OBSERVATION_SENSOR];
		[[NSNotificationCenter defaultCenter] postNotificationName:OBSERVATION_UPDATE object:self userInfo:note];
	}
}

- (void) awakeFromNib
{
	[[SensorsController sharedController] addSensor:SENSOR_NAME];

	[[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:@selector(update:) name:@"com.apple.iTunes.playerInfo"
		object:nil];
		
	[[NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(autoUpdate:) userInfo:nil repeats:NO] retain];
}

- (void) autoUpdate:(NSTimer *) theTimer
{
	[theTimer release];
	[self update:nil];
}

@end
